import {
    Component,
    ElementRef,
    EventEmitter,
    Injector,
    Output,
    ViewChild,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
    ParkingsBillingSchemeServiceProxy,
    CreateParkingsBillingScheme,
    GetBillingSchemeForEditOutput,
    ParkingsBillingSchemeEdit,
} from "@shared/service-proxies/service-proxies";
import { id } from "@swimlane/ngx-charts/release/utils";
import { ModalDirective } from "ngx-bootstrap";
import { SelectItem } from "primeng/api";
import { InputSwitchModule } from "primeng/inputswitch";
import { finalize } from "rxjs/operators";

@Component({
    selector: "createOrEditBillingSchemeModal",
    templateUrl: "./create-or-edit-billing-scheme-modal.component.html",
    styleUrls:["./create-or-edit-billing-scheme-modal.component.css"]
})
export class CreateOrEditBillingSchemeModalComponent extends AppComponentBase {
    @ViewChild("createOrEditModal") modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    getBillingSchemeForEditOutput: GetBillingSchemeForEditOutput =
        new GetBillingSchemeForEditOutput();
    parkingsBillingSchemeEdit: ParkingsBillingSchemeEdit =
        new ParkingsBillingSchemeEdit();

    active = false;
    saving = false;
    parkingCoce: string;
    parkingName: string;
    parkingid: number;
    neighbourhood_Name: string;
    neighbourhood_id: number;
    id: number;
    cars: SelectItem[];
    emptyText: "";
    selectedCar1: string;
    chargeType: number;
    val2: number;
    chargeTypeOption: any[] = [
        { label: "次数收费", value: 0 },
        { label: "时间收费", value: 1 }
    ];
    selectedChargeType:any = this.chargeTypeOption[0];
    
    constructor(
        injector: Injector,
        private _ParkingsBillingSchemeServiceProxy: ParkingsBillingSchemeServiceProxy
    ) {
        super(injector);   
        // this.cars = [
        //     { label: "时间收费", value: 0 },
        //     { label: "次数收费", value: 1 },
        // ];     
    }

    show(
        parkingCoce: string,
        parkingName: string,
        parkingid: number,
        neighbourhood_Name: string,
        neighbourhood_id: number,
        Id?: number
    ): void {
        const self = this;
        self.active = true;
        self.id = Id;
        self.parkingid = parkingid;
        self.neighbourhood_Name = neighbourhood_Name;
        self.parkingCoce = parkingCoce;
        self.parkingName = parkingName;
        self.neighbourhood_id = neighbourhood_id;
        self._ParkingsBillingSchemeServiceProxy
            .getBillingSchemeForEdit(Id)
            .subscribe((result) => {
                self.parkingsBillingSchemeEdit = result.billingSchemeEdit;                
                console.log(self.parkingsBillingSchemeEdit);  
                self.modal.show();             
            });        
    }

    onShown(): void {}

    save(): void {
        const self = this;

        // this.cars.forEach((element) => {
        //     if (element.label == this.selectedCar1) {
        //         this.chargeType == element.value;
        //     }
        // });
        const input = new CreateParkingsBillingScheme();
        (input.neighbourhood_name = this.neighbourhood_Name),
            (input.neighbourhood_id = this.neighbourhood_id),
            (input.parkingId = this.parkingid),
            (input.parkingName = this.parkingName),
            (input.parkingCode = this.parkingCoce),
            (input.cappedPrice = this.parkingsBillingSchemeEdit.cappedPrice),
            (input.planName = this.parkingsBillingSchemeEdit.planName),
            (input.remark = this.parkingsBillingSchemeEdit.remark),
            (input.chargeType = this.parkingsBillingSchemeEdit.chargeType),
            (input.isOpen = this.parkingsBillingSchemeEdit.isOpen),
            (input.freeTime = this.parkingsBillingSchemeEdit.freeTime),
            (input.intervalTime = this.parkingsBillingSchemeEdit.intervalTime),
            (input.univalent = this.parkingsBillingSchemeEdit.univalent),
            (input.onePrice = this.parkingsBillingSchemeEdit.onePrice),
            (input.monthlyRentPrice =
                this.parkingsBillingSchemeEdit.monthlyRentPrice),
            (input.quarterlyPrice =
                this.parkingsBillingSchemeEdit.quarterlyPrice),
            (input.halfYearPrice =
                this.parkingsBillingSchemeEdit.halfYearPrice),
            (input.yearPrice = this.parkingsBillingSchemeEdit.yearPrice),
            (input.sellingPrice = this.parkingsBillingSchemeEdit.sellingPrice),
            input.endFreeTime = this.parkingsBillingSchemeEdit.endFreeTime;
            (this.saving = true);
        console.log(input);
        
        if (this.id == null) {
            this._ParkingsBillingSchemeServiceProxy
                .create(input)
                .pipe(finalize(() => (this.saving = false)))
                .subscribe(() => {
                    this.notify.info(this.l("SavedSuccessfully"));
                    this.close();
                    this.modalSave.emit(null);
                });
        } else {
            input.id = this.parkingsBillingSchemeEdit.id;
            this._ParkingsBillingSchemeServiceProxy
                .update(input)
                .pipe(finalize(() => (this.saving = false)))
                .subscribe(() => {
                    this.notify.info(this.l("SavedSuccessfully"));
                    this.close();
                    this.modalSave.emit(null);
                });
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
        this.selectedCar1 = undefined;
    }
}
